/*****************************************************************************************************************
* 模块：Routing plan and Visit
* 开发人：Fu Haisha
* 描述：显示某个TSR的某天的路线计划的视图，可以切换到上一周和下一周
*/
public with sharing class ViewRoutingPlanControl {
        
        //public List<Routing_Plan_And_Visit__c> rpavList{get;set;}
        public List<RoutingPlan__c> rPlan{get;set;}
        public Date weekStart{get;set;}
        public List<Routing_Plan_And_Visit__c> rpavList{get;set;}
        public Date startDate2{get;set;}
        public Date endDate2{get;set;}
        public String yearId{get;set;}
        public String monthId{get;set;}
        public String tsrId{get;set;}
        public String test{get;set;}
    
    public List<SelectOption> listOption
    {
        get
        {
                if(listOption==null)
                        listOption=new List<SelectOption>();
                else
                        listOption.clear();
                        
                Integer y=Date.Today().year();
               
                for(Integer i=2010;i<=y+1;i++)
                {
                    if(i!=y)
                       listOption.add(new SelectOption(String.valueof(i),String.valueof(i)));
                    else
                       listOption.add(new SelectOption(String.valueof(i),String.valueof(i),false));

                }
               
                return listOption;
        }
        set;}
        
    public List<SelectOption> listOptionMonth
    {
        get
        {
                if(listOptionMonth==null)
                        listOptionMonth=new List<SelectOption>();
                else
                        listOptionMonth.clear();
                        
                 Integer Month=System.Today().month();
                 
                 for(Integer i=1;i<=12;i++)
                 {
                    if(i!=Month)
                        listOptionMonth.add(new SelectOption(String.valueOf(i),String.valueOf(i)));
                    else
                       listOptionMonth.add(new SelectOption(String.valueOf(i),String.valueOf(i),false));
 
                 }
                 
                return listOptionMonth;
        }set;
    }
    public List<SelectOption> listOptionTSR
    {
        get;set;
    }
    
        public ViewRoutingPlanControl()
        {
                if(listOptionTSR==null)
                        listOptionTSR=new List<SelectOption>();
                else
                        listOptionTSR.clear();
                        
                listOptionTSR.add(new SelectOption('0','Current User'));
                Profile p=[select Name from Profile where Id=: UserInfo.getProfileId()];test=p.Name;
                
                //如果是系统管理员加载所有的TSR否则加载所有的
                if(p.Name=='System Administrator' || p.Name=='系统管理员')
                {
                    for(User u:[select Id,Name from User where Title__c='Sales Rep' and FunctionDepartment__c='Trade Sales'])//添加所有TSR
                    {
                        listOptionTSR.add(new SelectOption(u.Id,u.Name));
                    }
                }
                else
                {
                   User u=[select Id,Name,Title__c,FunctionDepartment__c from User where Id=:UserInfo.getUserId()];
                   if(u.Title__c=='Area/District Sales Manager' || u.Title__c=='Sales Supervisor'
                   		 && (u.FunctionDepartment__c=='Trade Sales' || u.FunctionDepartment__c=='Trade&Medical Sales'))
                   {
                   	GetTsrId(u.Id);
                   	/*	List<Id> uSupList=new List<Id>();
                   		List<User> uList=[select Id,Name,Title__c,FunctionDepartment__c from User where ManagerId=:u.Id];
                   		
                   		for(User us:uList)
                   		{
                   			if(us.Title__c == 'Sales Rep' && us.FunctionDepartment__c=='Trade Sales')
                   			{
                   				listOptionTSR.add(new SelectOption(us.Id,us.Name));
                   			}
                   			else
                   			{
                   				uSupList.add(us.Id);
                   			}
                   		}
                   		
                   		if(uSupList.size()>0)
                   		{
                   			for(User u2:[select Id,Name,Title__c,FunctionDepartment__c from User where ManagerId in: uSupList])
                   			{
                   				if(u2.Title__c == 'Sales Rep' && u2.FunctionDepartment__c=='Trade Sales')
                   				{
                   					listOptionTSR.add(new SelectOption(u2.Id,u2.Name));
                   				}
                   			}
                   		}*/
                   		
                   	
                  /*   for(User u1:[select Id,Name,Title__c,FunctionDepartment__c from User where ManagerId=:u.Id])
                     {
                        if(u1.Title__c!='Sales Rep')
                        {
                            for(User u2:[select Id,Name,Title__c,FunctionDepartment__c from User where ManagerId=:u1.Id])
                            {
                                
                                if(u2.Title__c == 'Sales Rep' && u2.FunctionDepartment__c=='Trade Sales')
                                {
                                    listOptionTSR.add(new SelectOption(u2.Id,u2.Name));
                                }
                                    
                            }
                        }
                        else
                        {
                            listOptionTSR.add(new SelectOption(u1.Id,u1.Name));
                        }
                     }*/
                   }
                  /* else if(u.Title__c=='Sales Supervisor' && (u.FunctionDepartment__c=='Trade Sales' || u.FunctionDepartment__c=='Trade&Medical Sales'))
                   {
                        for(User u2:[select Id,Name,Title__c from User where ManagerId=:u.Id])
                        {
                                if(u2.Title__c == 'Sales Rep')
                                {
                                    listOptionTSR.add(new SelectOption(u2.Id,u2.Name));
                                }
                        }
                   }      */      
                }

                yearId=String.valueOf(System.Today().Year());
                monthId=String.valueOf(System.Today().Month());
                
            startDate2 =  Date.today().toStartOfWeek();
            endDate2 = startDate2.addDays(6);
            
        rpavList=GetData();
        
        }
        //递归找出下级的所有TSR
        public void GetTsrId(Id id)
        {
        	for(User u:[select Id,Name,Title__c,FunctionDepartment__c from User where ManagerId=:Id])
        	{
        		if(u.Title__c == 'Sales Rep' && u.FunctionDepartment__c=='Trade Sales')
        		{
        			listOptionTSR.add(new SelectOption(u.Id,u.Name));
        		}
        		else
        		{
        			GetTsrId(u.Id);
        		}
        	}
        }
        
        //切换到下一周
        public PageReference Next()
        {
           startDate2  = startDate2.addDays(7);
       endDate2 = endDate2.addDays(7);
       
       rpavList=GetData();
       
        return null;
        }
        //切换到上一周
        public PageReference Previous()
        {
                startDate2  = startDate2.addDays(-7);//-7
        endDate2 = endDate2.addDays(-7);//-7
        
        rpavList=GetData();
       
                return null;
        }
        public PageReference Searchs()
        {
                Integer year;
                Integer month;
                if(yearId==NULL)
                        year=Date.Today().year();
                else
                        year=Integer.valueof(yearId);
                
                if(monthId==NULL)
                        month=Date.Today().month();
                else
                        month=Integer.valueof(monthId);
                                
                Date d=Date.newInstance(year,month,1);
                startDate2  = d.toStartOfWeek();
                endDate2 = startDate2.addDays(6);
        
        rpavList=GetData();
        return null;
        }
        
        public List<Routing_Plan_And_Visit__c> GetData()
        {
                
                if(tsrId==NULL || tsrId=='0')
                        tsrId=UserInfo.getUserId();
                
             return [Select r.Name, r.Id,r.Visit_Date__C,r.OwnerId,(Select  id,Name,Sort__c,OutletName__r.OutletCode__c,OutletName__c From Routing_Plan__r order by Sort__c asc) 
                   From Routing_Plan_And_Visit__c r where  Visit_Date__c >= :startDate2 and Visit_Date__c<= :endDate2 and OwnerId=:tsrId order by Visit_Date__c asc ];
                
        }
        
        public PageReference News()
        {
             String Prefix = Routing_Plan_And_Visit__c.sObjectType.getDescribe().getKeyPrefix();   
             String tempURL = '/'+Prefix+'/e?retURL=/'+Prefix + '/o' ;
             PageReference TempRef = new PageReference(tempURL);
             TempRef.setRedirect(true);
             return TempRef;
        }
        public PageReference del()
        {
        
           delete [select id from Event where ownerId=:UserInfo.getUserId()];
           return null;
        }
       static testmethod void Test()
       {
            Profile p = [select id from profile where name='System Administrator'];
            User u = new User(alias = 'standtss', email='standarduser@testorgss.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,title__c='Area/District Sales Manager',
            FunctionDepartment__c='Trade Sales',
            timezonesidkey='America/Los_Angeles', username='standardusers@testorg.com');
            System.runAs(u)
            {
                ViewRoutingPlanControl v=new ViewRoutingPlanControl();
                
                v.startDate2=Date.toDay();
                v.endDate2=Date.toDay().addDays(2);
                v.listOption.add(new SelectOption('2008','2008'));
                v.listOptionMonth.add(new SelectOption('2','2'));
                v.listOptionTSR.add(new SelectOption(UserInfo.getUserId(),UserInfo.getUserName()));
                v.yearId='2010';
                v.monthId='2';
                v.tsrId=u.Id;
                v.Searchs();
                v.GetData();
                v.Previous();
                v.Next();
                v.News();
            }
       }
     
}